package yeqiang.util;

/**
 * Created by yeqiang on 3/26/19.
 */

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
public class InetUtils {

    private static final String[] HEADERS_TO_TRY = {
            "X-Forwarded-For",
            "Proxy-Client-IP",
            "WL-Proxy-Client-IP",
            "HTTP_X_FORWARDED_FOR",
            "HTTP_X_FORWARDED",
            "HTTP_X_CLUSTER_CLIENT_IP",
            "HTTP_CLIENT_IP",
            "HTTP_FORWARDED_FOR",
            "HTTP_FORWARDED",
            "HTTP_VIA",
            "REMOTE_ADDR"};

    /**
     * 从请求头中读取真实代理服务器前客户端ip地址
     *
     * @param request
     * @return
     */
    public static String getProxyRealIp(HttpServletRequest request) {
        String ip = null;
        for (String key : HEADERS_TO_TRY) {
            ip = request.getHeader(key);
            if (ip != null) {
                return ip;
            }
        }
        return ip;
    }


    /**
     * ipv4地址转长整形
     *
     * @param ip
     * @return
     */
    public static Long ip2long(String ip) {
        try {
            InetAddress address = InetAddress.getByName(ip);
            if (address instanceof Inet4Address) {
                long l = 0L;
                for (int i = 0; i < 4; i++) {
                    byte b = address.getAddress()[i];
                    long l1 = (long) b & 0xFF;
                    l += l1 << ((3 - i) * 8);
                }
                return l;
            }
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 将一个长整数转换成ipv4字符串
     *
     * @param lng
     * @return
     */
    public static String long2IP(Long lng) {
        byte[] array = new byte[4];
        for (int i = 0; i < 4; i++) {
            array[i] = (byte) ((lng >> ((3 - i) * 8)) & 0xFF);
        }
        try {
            String ipv4 = InetAddress.getByAddress(array).toString().substring(1);
            return ipv4;
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 将ipv6地址字符串转为大整数
     *
     * @param ip
     * @return
     */
    public static BigInteger ipv6ToBitInteger(String ip) {
        try {
            InetAddress address = InetAddress.getByName(ip);
            if (address instanceof Inet6Address) {
                return new BigInteger(1, address.getAddress());
            }
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 将大整数转换成ipv6字符串
     *
     * @param bigInteger
     * @return
     */
    public static String bigIntegerToIPv6(BigInteger bigInteger) {
        try {
            String ipv6 = InetAddress.getByAddress(bigInteger.toByteArray()).toString().substring(1);
            return ipv6;
        } catch (UnknownHostException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }


}
